const path = require('path')
const fs = require('fs')
const express = require('express')
const open = require('open')
const yargsParser = require('yargs-parser')

async function main() {
    // 参数 port host
    const buildDir = path.join(process.cwd(), 'build')

    if (!fs.existsSync(buildDir)) {
        return console.log('Please run "ddocs build" first.')
    }

    const argv = yargsParser(process.argv.slice(2))
    const port = argv.p || 8080
    const host = argv.h || 'localhost'
    const url = `http://${host}:${port}/`

    const app = express()

    app.use(express.static(buildDir))

    app.listen(port, () => {
        console.log(`you can visit: ${url}`)
        open(url)
    })
}

module.exports = main